package com.dq.service.messsge.impl;

import com.alibaba.fastjson.JSONObject;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.InstructionMessageQuery;
import com.dq.vo.Vo;
import com.dq.domain.message.oneself.InstructionMessage;
import com.dq.domain.message.oneself.NoticeMessage;
import com.dq.domain.message.oneself.centre.InstructionStaffuserCentre;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.messsge.InstructionMessageService;
import com.dq.service.sys.user.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 *
 *
 */
@Service
public class InstructionMessageServiceImpl extends BaseRepositories implements InstructionMessageService {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取 boss 创建 的指令列表
     * @param instructionMessageQuery
     * @return
     * @throws AdminException
     */
    @Override
    public Vo getDisposeByBoss(@ApiParam("createUserId: 创建人ID") InstructionMessageQuery instructionMessageQuery) throws AdminException {
        instructionMessageQuery.format();
        Map<String, Object> query = instructionMessageQuery.parseMap();
        if(instructionMessageQuery.getCreateUserId() != null) {
            query.put("createUser_id", instructionMessageQuery.getCreateUserId());
            query.remove("createUserId");
        }
        if(instructionMessageQuery.getOrgId() != null) {
            query.put("org_id", instructionMessageQuery.getOrgId());
            query.remove("orgId");
        }

        List<JSONObject> returnJson = new ArrayList<>();
        List<InstructionMessage> list = dao.find(dao.query2Hql(InstructionMessage.class, query), query);
        for (InstructionMessage instructionMessage : list) {
            Map<String, Object> query01 = new HashMap<>(16);
            query01.put("instructionMessage_id", instructionMessage.getId());
            List<InstructionStaffuserCentre> centreList = dao.find(dao.query2Hql(InstructionStaffuserCentre.class, query01), query01);
            returnJson.add(instructionMessage.toJson(centreList, null, null));
        }

        Long count = dao.count(dao.count2Hql(InstructionMessage.class, query), query);
        PageHelper.startPage(instructionMessageQuery.getPage(), instructionMessageQuery.getSize());
        PageInfo pageInfo = new PageInfo(returnJson);
        pageInfo.setTotal(count);

        return new Vo(pageInfo);
    }

    /**
     * boss 添加 指令消息
     */
    @Override
    public InstructionMessage save(InstructionMessage instructionMessage)  throws AdminException {
        // 注： 前台传来 主题
        instructionMessage.setMessageStatus(NoticeMessage.MessageStatus.noSee);
        instructionMessage.setMessageType(NoticeMessage.MessageType.instruction);

        instructionMessage.setDeleted(false);

        SysUser user = sysUserService.getUser();
        instructionMessage.setCreateUser(user);
        instructionMessage.setUpdateUser(user);

        // todo bossOrg boss发布指令的关联门店用父类，不用于搜索
        instructionMessage.setOrg(user.getSection());

        // 实体初始 判断
        judgeObject(instructionMessage);

        return instructionMessageRepository.save(instructionMessage);
    }

    /**
     *修改 指令消息
     */
    @Override
    public InstructionMessage update(InstructionMessage instructionMessage) throws AdminException {
        instructionMessage.setLastModifyTime(System.currentTimeMillis());

        SysUser user = sysUserService.getUser();
        if(user != null) {
            instructionMessage.setUpdateUser(user);
        }

        return instructionMessageRepository.save(instructionMessage);
    }

    /**
     * 实体初始 判断
     */
    public void judgeObject(InstructionMessage instructionMessage) throws AdminException {
        if(StringUtils.isBlank(instructionMessage.getTheme())) {
            throw new AdminException("主题不能为空");
        }
        if(StringUtils.isBlank(instructionMessage.getContent())) {
            throw new AdminException("内容不能为空");
        }
//        if(instructionMessage.getOrg() == null) {
//            throw new AdminException("关联门店不能为空");
//        }
        if(instructionMessage.getBeginTimeRestrict() == null) {
            throw new AdminException("开始时间不能为空");
        }
        if(instructionMessage.getNedTimeRestrict() == null) {
            throw new AdminException("结束时间不能为空");
        }
        if(StringUtils.isBlank(instructionMessage.getAnnexUrl())) {
            throw new AdminException("附件地址不能为空");
        }
        if(StringUtils.isBlank(instructionMessage.getImageUrl())) {
            throw new AdminException("图片地址不能为空");
        }
    }

}

